package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.PermissionDao;
import com.itheima.dao.RoleDao;
import com.itheima.dao.UserDao;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 用户服务
 */
@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;
    //根据用户名查询数据库获取用户信息和关联的角色信息，同时需要查询角色关联的权限信息
    public User findByUsername(String username) {
        User user = userDao.findByUsername(username);//查询用户基本信息，不包含用户的角色
        if(user == null){
            return null;
        }
        Integer userId = user.getId();
        //根据用户ID查询对应的角色
        Set<Role> roles = roleDao.findByUserId(userId);
        for (Role role : roles) {
            Integer roleId = role.getId();
            //根据角色ID查询关联的权限
            Set<Permission> permissions = permissionDao.findByRoleId(roleId);
            role.setPermissions(permissions);//让角色关联权限
        }
        user.setRoles(roles);//让用户关联角色
        return user;
    }


    //查询所有的用户;
    @Override
    public List<User> findAll() {
        return userDao.findAll();
    }

    @Override
    public void addUser(User user,Integer[] checkRoleid) {
        //添加用户
        userDao.addUser(user);
        //添加到中间表；
        Integer id = user.getId();

        this.setRelationWithUserAndRole(id,checkRoleid);


    }
    //根据用户id删除用户;
    @Override
    public void deleteById(int id) {
        //先删除中间表的信息 再删除 用户表;
        roleDao.deleteRelationBetweenUserAndRole(id);
        //删除user表中的数据;
        userDao.deleteById(id);


    }

    @Override
    public User findById(int id) {
        return  userDao.findById(id);
    }

    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {


        return roleDao.findRoleIdsByUserId(id);
    }

    @Override
    public void updateUser(User user, Integer[] roleIds) {
        //修改用户;
        //先删除中间关系表;
        for (Integer roleId : roleIds) {
            roleDao.deleteRelationBetweenUserAndRole(roleId);
        }
        //然后修改user表
        userDao.updateUser(user);
        //然后添加中间表数据;
        Integer id = user.getId();
        this.setRelationWithUserAndRole(id,roleIds);



    }

    private void setRelationWithUserAndRole(Integer id, Integer[] rowlds) {
        if (rowlds!=null&&rowlds.length>0){
            //封装一个map；
            for (Integer rowld : rowlds) {
                Map<String,Integer> map=new HashMap<>();
                map.put("user_id",id);
                map.put("role_id",rowld);
                roleDao.addRelation(map);
            }


        }


    }
}
